package pzl.com.canvasapi.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import pzl.com.canvasapi.R;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-18]
 */
public class V_SaveLayer2 extends View {
    Bitmap b1, b2, b3;


    public V_SaveLayer2(Context context) {
        super(context);
    }

    public V_SaveLayer2(Context context, AttributeSet attrs) {
        super(context, attrs);
        b1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
        b2 = Bitmap.createBitmap(b1);
        b3 = Bitmap.createBitmap(b1);
    }

    public V_SaveLayer2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(getCircleBitmap(b1, 280), 0, 0, new Paint());

        canvas.drawBitmap(getRoundedCornerBitmap(b2, 80, getWidth() / 2, 300), 0, 300, new Paint());

        canvas.drawBitmap(getRoundCornerImage(b3, 20, 200, 200), 0, 620, new Paint());
    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx, int displayW, int displayH) {
        int x = bitmap.getWidth();
        int y = bitmap.getHeight();
        x = displayW;
        y = displayH;
        float[] mOuter = new float[]{roundPx, roundPx, roundPx, roundPx,
                roundPx, roundPx, roundPx, roundPx};
        // 根据源文件新建一个darwable对象
        Drawable imageDrawable = new BitmapDrawable(bitmap);

        // 新建一个新的输出图片
        Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        // 新建一个矩形
        RectF outerRect = new RectF(0, 0, x, y);
        Path mPath = new Path();
        // 创建一个圆角矩形路径
        mPath.addRoundRect(outerRect, mOuter, Path.Direction.CW);

        // 画出一个红色的圆角矩形
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        canvas.drawPath(mPath, paint);
        // 设置绘图两层交汇时的模式
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        // 设置绘图区域（在那个区域内绘图）
        imageDrawable.setBounds(0, 0, x, y);
        // 将用于绘制图片的图层拷贝到单独图层并压入图层栈
        canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
        // 将源图片绘制到这个拷贝的图片图层上
        imageDrawable.draw(canvas);
        // 将图片图层退出栈并将图像绘制到矩形的那一层
        canvas.restore();
        return output;
    }


    public static Bitmap getCircleBitmap(Bitmap bitmap, float roundPx) {
        Drawable imageDrawable = new BitmapDrawable(bitmap);
        // 新建一个新的输出图片
        Bitmap output = Bitmap.createBitmap((int) roundPx, (int) roundPx, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Path mPath = new Path();
        mPath.addCircle(roundPx / 2, roundPx / 2, roundPx / 2, Path.Direction.CW);

        // 画出一个红色的圆角矩形
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        canvas.drawPath(mPath, paint);
        // 设置绘图两层交汇时的模式
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        // 设置绘图区域（在那个区域内绘图）
        imageDrawable.setBounds(0, 0, (int) roundPx, (int) roundPx);
        // 将用于绘制图片的图层压入图层栈
        canvas.saveLayer(0, 0, (int) roundPx, (int) roundPx, paint, Canvas.ALL_SAVE_FLAG);
        // 将源图片绘制到这个圆角矩形上
        imageDrawable.draw(canvas);
        // 将图片图层退出栈并将图像绘制到矩形的那一层
        canvas.restore();
        return output;
    }


    public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels, int displayW, int displayH) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        w = displayW;
        h = displayH;
        // 创建一个和原始图片一样大小位图
        Bitmap roundConcerImage = Bitmap.createBitmap(w,
                h, Bitmap.Config.ARGB_8888);
        // 创建带有位图roundConcerImage的画布
        Canvas canvas = new Canvas(roundConcerImage);
        // 创建画笔
        Paint paint = new Paint();
        // 创建一个和原始图片一样大小的矩形
        Rect rect = new Rect(0, 0, w, h);
        RectF rectF = new RectF(rect);
        // 去锯齿
        paint.setAntiAlias(true);
        // 画一个和原始图片一样大小的圆角矩形
        canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);
        // 设置相交模式
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        // 把图片画到矩形去
        canvas.drawBitmap(bitmap, null, rect, paint);
        return roundConcerImage;
    }

}
